"""
@Author: li
@Email: lijianqiao2906@live.com
@FileName: base.py
@DateTime: 2025/07/05
@Docs: 基础服务类
"""

from typing import Any

from tortoise.models import Model

from app.dao.base import BaseDAO


class BaseService:
    """基础服务类，使用DAO层进行数据操作"""

    def __init__(self, model: type[Model]):
        self.model = model
        self.dao = BaseDAO(model)

    async def get_by_id(self, id: int) -> Model | None:
        """根据ID获取单个对象"""
        return await self.dao.get_by_id(id)

    async def get_by_ids(self, ids: list[int]) -> list[Model]:
        """根据ID列表批量获取对象"""
        return await self.dao.get_by_ids(ids)

    async def get_one(self, **filters) -> Model | None:
        """根据条件获取单个对象"""
        return await self.dao.get_one(**filters)

    async def get_all(self, **filters) -> list[Model]:
        """获取所有对象"""
        return await self.dao.get_all(**filters)

    async def exists(self, **filters) -> bool:
        """检查对象是否存在"""
        return await self.dao.exists(**filters)

    async def count(self, **filters) -> int:
        """获取对象数量"""
        return await self.dao.count(**filters)

    async def create(self, **data) -> Model | None:
        """创建对象"""
        return await self.dao.create(**data)

    async def bulk_create(self, objects_data: list[dict[str, Any]]) -> list[Model]:
        """批量创建对象"""
        return await self.dao.bulk_create(objects_data)

    async def update(self, id: int, **data) -> Model | None:
        """更新对象"""
        return await self.dao.update_by_id(id, **data)

    async def bulk_update(self, updates: list[dict[str, Any]], id_field: str = "id") -> int:
        """批量更新对象"""
        return await self.dao.bulk_update(updates, id_field)

    async def update_by_filter(self, filters: dict[str, Any], **data) -> int:
        """根据条件批量更新"""
        return await self.dao.update_by_filter(filters, **data)

    async def delete(self, id: int) -> bool:
        """删除对象（默认软删除）"""
        return await self.dao.delete_by_id(id)

    async def delete_by_ids(self, ids: list[int]) -> int:
        """批量删除对象（默认软删除）"""
        return await self.dao.delete_by_ids(ids)

    async def delete_by_filter(self, **filters) -> int:
        """根据条件批量删除（默认软删除）"""
        return await self.dao.delete_by_filter(**filters)

    async def get_paginated(
        self, page: int = 1, page_size: int = 10, order_by: list[str] | None = None, **filters
    ) -> tuple[list[Model], int]:
        """分页获取对象"""
        return await self.dao.get_paginated(page, page_size, order_by, **filters)

    async def get_or_create(self, defaults: dict[str, Any] | None = None, **kwargs) -> tuple[Model, bool]:
        """获取或创建对象"""
        return await self.dao.get_or_create(defaults, **kwargs)

    async def bulk_upsert(
        self, objects_data: list[dict[str, Any]], conflict_fields: list[str], update_fields: list[str] | None = None
    ) -> list[Model]:
        """批量插入或更新"""
        return await self.dao.bulk_upsert(objects_data, conflict_fields, update_fields)

    # 软删除方法
    async def soft_delete(self, id: int) -> bool:
        """软删除对象（标记为已删除）"""
        return await self.dao.soft_delete_by_id(id)

    async def soft_delete_by_ids(self, ids: list[int]) -> int:
        """批量软删除对象"""
        return await self.dao.soft_delete_by_ids(ids)

    async def soft_delete_by_filter(self, **filters) -> int:
        """根据条件批量软删除"""
        return await self.dao.soft_delete_by_filter(**filters)

    # 硬删除方法（物理删除）
    async def hard_delete(self, id: int) -> bool:
        """硬删除对象（物理删除）"""
        return await self.dao.hard_delete_by_id(id)

    async def hard_delete_by_ids(self, ids: list[int]) -> int:
        """批量硬删除对象"""
        return await self.dao.hard_delete_by_ids(ids)

    async def hard_delete_by_filter(self, **filters) -> int:
        """根据条件批量硬删除"""
        return await self.dao.hard_delete_by_filter(**filters)

    # 恢复方法
    async def restore(self, id: int) -> bool:
        """恢复软删除的对象"""
        return await self.dao.restore_by_id(id)

    async def restore_by_ids(self, ids: list[int]) -> int:
        """批量恢复软删除的对象"""
        return await self.dao.restore_by_ids(ids)

    # 查询增强方法
    async def get_active_by_id(self, id: int) -> Model | None:
        """根据ID获取未删除的对象"""
        return await self.dao.get_active_by_id(id)

    async def get_active_all(self, **filters) -> list[Model]:
        """获取所有未删除的对象"""
        return await self.dao.get_active_all(**filters)

    async def get_deleted_all(self, **filters) -> list[Model]:
        """获取所有已软删除的对象"""
        return await self.dao.get_deleted_all(**filters)

    async def count_active(self, **filters) -> int:
        """统计未删除对象的数量"""
        return await self.dao.count_active(**filters)

    async def count_deleted(self, **filters) -> int:
        """统计已软删除对象的数量"""
        return await self.dao.count_deleted(**filters)

    async def get_active_paginated(
        self, page: int = 1, page_size: int = 10, order_by: list[str] | None = None, **filters
    ) -> tuple[list[Model], int]:
        """分页获取未删除的对象"""
        return await self.dao.get_active_paginated(page, page_size, order_by, **filters)
